package com.zidian.study.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.PopupMenu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.zidian.study.fragment.CourseFragment;
import com.zidian.study.fragment.EvaluateFragment;
import com.zidian.study.fragment.InteractionFragment;
import com.zidian.study.fragment.PersonFragment;
import com.zidian.study.fragment.TaskFragment;
import com.zidian.study.globa.ActManager;
import com.zidian.study.globa.AppActivity;
import com.zidian.study.globa.BaseFragment;
import com.zidian.study.utils.DateUtil;
import com.zidian.study.utils.SharedPreferenceUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.ShareSDK;
import com.zidian.student.trucks.R;

public class MainActivity extends AppActivity {

    private boolean isShowMsg;
    /* ============Model============== */
    private PopupMenu popupMenu;
    private Menu menu;
    private final static String[] NOTICE_MSG = {"从零开始的吱吱","万水千山总是情，吱波老师行不行","不想上课，是病，得吱",
            "来吱吱秀得了我老师","厉害了，word吱", "别忘咯，要吱得飞起","人生苦短必须吱吱"," 且听我吱一波",
            "没有被吱吱的老师是不完整的","没时间解释了，来吱吱吧！","我的心愿是..吱吱全场..","前方吱吱预警","吱一下不过三秒",
            "扶我起来，我还能再吱一下","给你吱一下的机会","给你吱一下的机会","吱得飞起","先定一个小目标，比方说我先吱它一波",
            "噢！在这狂吱","让老师感受被吱配的恐惧吧","喵的，吱障","抑制不住洪荒之力，现在就吱一下"};
    /**
     * 判断跳转到哪个页面的下标
     */
    private int switchid = 0;
    /**
     * Fragment管理器
     */
    private FragmentManager fragmentManager;
    /**
     * 界面下方四个单选按钮组
     */
    private RadioGroup radio_group;
    /* ============Control================= */
    /**
     * 四个按钮的单击监听
     */
    private RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup arg0, int keyCode) {
            /* 处理之前的文字 */
            ((RadioButton) arg0.getChildAt(switchid)).setTextColor(0xff7f7f7c);
            /* 处理被单击的按钮 */
            RadioButton mRadioButton = (RadioButton) arg0.findViewById(keyCode);
            mRadioButton.setTextColor(0xff25c17f);

            switchid = Integer.parseInt(mRadioButton.getTag().toString());
            /* 设置跳转那个fragment页面 */
            switch (switchid) {
                case 0:
                    setTitle("课程几乎");
                    addFragment(CourseFragment.newInstance());
                    break;
                case 1:
                    setTitle("互动");
                    addFragment(InteractionFragment.newInstance());
                    break;
                case 2:
                    setTitle("评价");
                    addFragment(EvaluateFragment.newInstance());
                    break;
                case 3:
                    setTitle("任务");
                    addFragment(TaskFragment.newInstance());
                    break;
                case 4:
                    setTitle("我");
                    addFragment(PersonFragment.newInstance());
                    break;
            }

        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        isShowMsg = intent.getBooleanExtra("isShowNoticeMsg",false);
        radio_group = (RadioGroup) findViewById(R.id.radio_group);
        radio_group.setOnCheckedChangeListener(mOnCheckedChangeListener);
        ((RadioButton) radio_group.getChildAt(0)).setChecked(true);

        ShareSDK.initSDK(this);
        //初始化JPush
        JPushInterface.init(getApplicationContext());
    }


    @Override
    protected BaseFragment getFirstFragment() {
        return CourseFragment.newInstance();
    }

    /**
     * 每天弹出一次不同的提醒
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (isShowMsg) {
            Toast.makeText(this,getRandomMsg(),Toast.LENGTH_SHORT).show();
            isShowMsg = false;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
        }
        return false;
    }

    // 双击退出程序
    private boolean isExit = false;

    private void exit() {
        if (!isExit) {
            isExit = true;
            Snackbar.make(radio_group,"再按一次退出程序",Snackbar.LENGTH_SHORT).show();
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            // 退出
            ActManager.getAppManager().AppExit(context);
        }
    }

    /**
     * 随机获取提醒信息
     * @return
     */
    private String getRandomMsg() {
        return NOTICE_MSG[(int) (Math.random()*NOTICE_MSG.length)];
    }
    /**
     * 根据星期获取提醒信息（暂时不用）
     * @return
     */
    private String getNoticeMsg() {
        Date time = new Date();
        time = Calendar.getInstance().getTime();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(time);

        int intTemp = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        return NOTICE_MSG[intTemp];
    }

    /**
     * 定时判断是否弹出提醒（暂时不用）
     * @return
     */
    private boolean isShowNoticeMsg(){
        int offHour = DateUtil.getOffectHour(SharedPreferenceUtils.getDate(),System.currentTimeMillis());
        if (Math.abs(offHour) > 12 ) {
            SharedPreferenceUtils.setDate(System.currentTimeMillis());
            return true;
        } else
            return false;
    }

}
